package com.ruoyi.aiexperiments.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.aiexperiments.domain.AiEthicsReview;
import com.ruoyi.aiexperiments.service.IAiEthicsReviewService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 实验伦理性审核Controller
 * 
 * @author czj
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/review")
public class AiEthicsReviewController extends BaseController
{
    @Autowired
    private IAiEthicsReviewService aiEthicsReviewService;

    /**
     * 查询实验伦理性审核列表
     */
    @RequiresPermissions("aiexperiments:review:list")
    @GetMapping("/list")
    public TableDataInfo list(AiEthicsReview aiEthicsReview)
    {
        startPage();
        aiEthicsReview.setDeleteFlag(0);
        List<AiEthicsReview> list = aiEthicsReviewService.selectAiEthicsReviewList(aiEthicsReview);
        return getDataTable(list);
    }

    /**
     * 导出实验伦理性审核列表
     */
    @RequiresPermissions("aiexperiments:review:export")
    @Log(title = "实验伦理性审核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AiEthicsReview aiEthicsReview)
    {
        List<AiEthicsReview> list = aiEthicsReviewService.selectAiEthicsReviewList(aiEthicsReview);
        ExcelUtil<AiEthicsReview> util = new ExcelUtil<AiEthicsReview>(AiEthicsReview.class);
        util.exportExcel(response, list, "实验伦理性审核数据");
    }

    /**
     * 获取实验伦理性审核详细信息
     */
    @RequiresPermissions("aiexperiments:review:query")
    @GetMapping(value = "/{reviewId}")
    public AjaxResult getInfo(@PathVariable("reviewId") Long reviewId)
    {
        return success(aiEthicsReviewService.selectAiEthicsReviewByReviewId(reviewId));
    }

    /**
     * 新增实验伦理性审核
     */
    @RequiresPermissions("aiexperiments:review:add")
    @Log(title = "实验伦理性审核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AiEthicsReview aiEthicsReview)
    {
        aiEthicsReview.setCreatedBy(SecurityUtils.getUsername());
        aiEthicsReview.setCreatedAt(new Date());
        return toAjax(aiEthicsReviewService.insertAiEthicsReview(aiEthicsReview));
    }

    /**
     * 修改实验伦理性审核
     */
    @RequiresPermissions("aiexperiments:review:edit")
    @Log(title = "实验伦理性审核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AiEthicsReview aiEthicsReview)
    {
        aiEthicsReview.setUpdatedBy(SecurityUtils.getUsername());
        aiEthicsReview.setUpdatedAt(new Date());
        return toAjax(aiEthicsReviewService.updateAiEthicsReview(aiEthicsReview));
    }

    /**
     * 删除实验伦理性审核
     */
    @RequiresPermissions("aiexperiments:review:remove")
    @Log(title = "实验伦理性审核", businessType = BusinessType.DELETE)
	@DeleteMapping("/{reviewIds}")
    public AjaxResult remove(@PathVariable Long[] reviewIds)
    {
        for (Long reviewId : reviewIds) {
            AiEthicsReview aiEthicsReview = aiEthicsReviewService.selectAiEthicsReviewByReviewId(reviewId);
            if (aiEthicsReview == null) {
                return error("实验伦理性审核不存在");
            }
            aiEthicsReview.setDeletedBy(SecurityUtils.getUsername());
            aiEthicsReview.setDeletedAt(new Date());
            aiEthicsReview.setDeleteFlag(1);
            aiEthicsReviewService.updateAiEthicsReview(aiEthicsReview);
        }

        //return toAjax(aiEthicsReviewService.deleteAiEthicsReviewByReviewIds(reviewIds));
        return toAjax(true);
    }
}
